#include "head.h"

int child_op(ptrain_t pt)
{
    int fd=pt->fd;
    while(1){
        bzero(pt->buf,sizeof(pt->buf));
        recv(fd,&pt->data_len,sizeof(int),0);
       if(pt->data_len==0)
		{
			printf("CLIENT EXIT!\n");
			close(pt->fd);
			free(pt);
			exit(1);
		}
		recvn(fd,pt->buf,pt->data_len);
		if(strncmp("cd",pt->buf,2) == 0)
		{
			//printf("this is cd\n");
			do_cd(pt);
		}else if(strncmp("ls",pt->buf,2) == 0)
		{
			do_ls(pt);
		}else if( strncmp("puts",pt->buf,4)== 0)
		{
			do_puts(pt);
		}else if( strncmp("gets",pt->buf,4)== 0)
		{
			do_gets(pt);

		}else if( strncmp("rm",pt->buf,2)== 0)
		{
			do_rm(pt);

		}else if(strncmp("pwd",pt->buf,3) == 0) 
		{
			do_pwd(pt);

		}else 
		{
			continue ;
		}
    }
    return 0;
}
int recvn(int fd_recv,char* recv_buf, int len)
{
	int sum = 0 ;
	int nrecv ;
	while(sum < len)
	{
		nrecv = recv(fd_recv, &recv_buf[sum], len - sum, 0);
		sum += nrecv ;
	}
	recv_buf[sum] = '\0';
	return sum ;
}
int sendn(int fd_send, char* send_buf, int len)
{
	int sum = 0 ;
	int nsend ;
	while(sum < len)
	{
		nsend = send(fd_send, send_buf + sum, len - sum, 0);
		sum += nsend ;
	}
	return sum ;
}